﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using PassBag;
using PassBag.Data;
using PassBag.Logic;

namespace TestPassBag {
    [TestFixture, Description ("Test XML serialization for PasswordObject")]
    public class TestXmlSerialization {
        private const string OUTPUT_DIRECTORY = "output";
        private const string OUTPUT_FILE = "output.xml";
        private const int NUMBER_OF_TEST = 200;

        [SetUp, Description ("Prepare directory for output")]
        public void PrepareOutputDirectory () {
            if (System.IO.Directory.Exists(OUTPUT_DIRECTORY)) {
		        System.IO.Directory.Delete(OUTPUT_DIRECTORY, true);
	        }
            System.IO.Directory.CreateDirectory (OUTPUT_DIRECTORY);
        }

        [TearDown, Description ("Clear directory content")]
        public void ClearOutputDirectory () {
            if (System.IO.Directory.Exists (OUTPUT_DIRECTORY)) {
                System.IO.Directory.Delete (OUTPUT_DIRECTORY, true);
            }
        }

        [Test, Description ("Generic password list serialization and deserialization")]
        public void XmlGenericListSerialization () {
            int i;
            Guid guid1 = Guid.NewGuid ();
            string string1 = "Hello1234567890'^qwertzuiopèäsdfghjkléà$yxcvbnm,.-;:_öä£!ü`?=)(/&%ç*+\\\"";
            DateTime date1 = DateTime.MinValue;
            Guid guid2 = Guid.NewGuid ();
            string string2 = "Hello World ! 1234567890'^qwertzuiopèäsdfghjkléà$yxcvbnm,.-;:_öä£!ü`?=)(/&%ç*+\\\"";
            DateTime date2 = DateTime.MaxValue;

            List<PasswordObject> source = new List<PasswordObject> ();

            for (i = 0; i < NUMBER_OF_TEST; i++) {
                source.Add (new PasswordObject (guid1,
                                                      string1,
                                                      string1,
                                                      string1,
                                                      string1,
                                                      string1,
                                                      string1,
                                                      date1));
            }

            for (i = 0; i < NUMBER_OF_TEST; i++) {
                source.Add (new PasswordObject (guid2,
                                                      string2,
                                                      string2,
                                                      string2,
                                                      string2,
                                                      string2,
                                                      string2,
                                                      date2));
            }

            ObjectSerializer.SerializeObject<List<PasswordObject>> (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + OUTPUT_FILE, source);

            List<PasswordObject> destination = ObjectSerializer.DeserializeObject<List<PasswordObject>> (OUTPUT_DIRECTORY + System.IO.Path.DirectorySeparatorChar + OUTPUT_FILE);

            for (i = 0; i < (NUMBER_OF_TEST*2); i++) {
                //Assert.AreEqual (source[i].Id, destination[i].Id); // disabled because contain XmlIgnore 
                Assert.AreEqual (source[i].Information, destination[i].Information);
                Assert.AreEqual (source[i].Title, destination[i].Title);
                Assert.AreEqual (source[i].UserName, destination[i].UserName);
                Assert.AreEqual (source[i].Password, destination[i].Password);
                Assert.AreEqual (source[i].Url, destination[i].Url);
                Assert.AreEqual (source[i].Comment, destination[i].Comment);
                //Assert.AreEqual (source[i].LastModified, destination[i].LastModified); // disabled because contain XmlIgnore
            }


        }

    }
}
